package com.jiyun.controller;


import cn.hutool.http.server.HttpServerRequest;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.R;
import com.jiyun.Result;
import com.jiyun.ResultClasss;
import com.jiyun.fegin.*;
import com.jiyun.pojo.day01.Member;
import com.jiyun.pojo.day02.*;
import com.jiyun.pojo.day05.Compet;
import com.jiyun.pojo.day05.CompetDTO;
import com.jiyun.pojo.day06.PaperDTO;
import com.jiyun.service.CourseService;
import com.sun.net.httpserver.Headers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author weiyuanhang
 * @data: 2023/8/23 002316:40
 * @Package cn.jiyun.controller
 * @email 3185757326@qq.com
 */
@RestController
@RequestMapping("/mycourse")
public class CourseController {
    @Autowired
    private CourseFigenService courseFigenService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ContestFigenService contestFigenService;
    @Autowired
    private TagsService tagsService;

    @Autowired
    private DirectoryFeginService directoryFeginService;
    @Autowired
    private  PaperFigen paperFigen;


    @GetMapping("/findPaper")
    public  Result findPaper(){
        return paperFigen.findPaper();
    }

   @GetMapping("/findTopic")
   public  Result findTopic(Integer pid){
        return paperFigen.findTopic(pid);
   }

    @PostMapping("/save1")
    public  Result save1(@RequestBody Compet compet){
        return contestFigenService.save(compet);
    }
    @PostMapping("findZXAll")
    public Result findZXAll(@RequestBody CompetDTO competDTO){
        return contestFigenService.findZXAll(competDTO);
    }




    @GetMapping("/xueDirectory")
    private ResultClasss xueDirectory(Integer cid, Integer uid, Integer did){
        return directoryFeginService.xueDirectory(cid,uid,did);
    }


    @GetMapping("/findTowDitectoryNum")
    private ResultClass findTowDitectoryNum(Integer cid){
        Integer num=directoryFeginService.findTowDitectoryNum(cid);
        return ResultClass.ok(num);
    }
    @GetMapping("/findUserOverDirectory")
    private ResultClass findUserOverDirectory(Integer cid,Integer uid){
        List<CourseDistory> list=directoryFeginService.findOverDirectoryNum(cid,uid);
        return ResultClass.ok(list);
    }

    @GetMapping("/findDitectoryList")
    private ResultClass findDitectoryList(Integer cid){
        List<DirectoryResult> ditectoryList = directoryFeginService.findDitectoryList(cid);
        return ResultClass.ok(ditectoryList);
    }


    @RequestMapping("/list")
    public R list(){
        return tagsService.list();
    }
    @RequestMapping("/findCourse2")
    public  Result findCourse2(@RequestBody CourseDTO courseDTO){
        return courseFigenService.findCourse2(courseDTO);
    }

    @GetMapping("/findBaCourse")
    public Integer findBaCourse(){
        return  courseFigenService.findCountId();
    }

    @GetMapping("/findByContest")
    public Integer findByContest(){
        return contestFigenService.findByCount();
    }

    @GetMapping("/findCourse")
    public Result findCourse(){
        return courseFigenService.findCourse();
    }

    @RequestMapping("/findZXCourse")
    public  Result findZXCourse(@RequestBody CourseDTO courseDTO){
        return  courseFigenService.findZXCourse(courseDTO);
    }

    @RequestMapping("/findZRCourse")
    public  Result findZRCourse(@RequestBody CourseDTO courseDTO){
        return courseFigenService.findZRCourse(courseDTO);
    }

    // @RequestMapping("/adddz")
    //     public ResultClasss adddz(Integer cid, HttpServletRequest request){
    //     String token1 = request.getHeader("token");
    //
    //     // String token1 = request.getHeader("token");
    //     // String token = request.getHeader("token");
    //     User user = JSON.parseObject(String.valueOf(redisTemplate.opsForHash().get("用户信息", token1)), User.class);
    //     String o = (String) redisTemplate.opsForHash().get("课程点赞", String.valueOf(cid));
    //     redisTemplate.opsForHash().put("课程点赞",String.valueOf(cid),o+"uid:"+user.getId());
    //
    //     return ResultClasss.ok(1).Message("点赞成功");
    // }

    @RequestMapping("/adddz")
    public  Result adddz(Integer cid, HttpServletRequest request) {
        // QueryWrapper<Course> wrapper = new QueryWrapper<>();
        // wrapper.eq("cid", cid);
        // Course course = courseService.getOne(wrapper);
        // if (course.getDz() == course.getDz()) {
        //     course.setDz(course.getDz() + 1);
        //     courseService.updateById(course);
        //     List<Course> list = courseService.list();
        //     for (Course course1 : list) {
        //         Course course2 = courseService.getById(course1.getCid());
        //         if (course2.getDz() == course1.getDz()) {
        //             course2.setDz(course2.getDz() - 1);
        //             courseService.updateById(course2);
        //         }
        //     }
        // }
        //先根据课程id  去查询是否点过赞
        String s = (String) redisTemplate.opsForHash().get("课程点赞", cid.toString());
//        String  s = (String) redisTemplate.opsForValue().get(id.toString());
        if (!StringUtils.isBlank(s)){
            //获取当前登录人
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s1 = (String) redisTemplate.opsForHash().get("用户信息",token);
            Member member = JSONObject.parseObject(s1, Member.class);
            //如果查询到   判断点赞的eid中有没有当前登录人的id  如果
            if (s.contains("mid"+member.getMid())){
                //有 就是已经点过赞  进行取消点赞操作  删除这个eid的记录
                String s2 = s.replace("mid"+member.getMid(), "");
                //存到redis中
                redisTemplate.opsForHash().put("课程点赞",cid.toString(),s2);
                return Result.ok().message("取消点赞成功");
            }else{
                //该用户没有点过赞  进行点赞操作 将当前登录人的eid拼接到点赞的字符串中
                String s2 = s + "mid"+member.getMid();
                //存到redis中
                redisTemplate.opsForHash().put("课程点赞",cid.toString(),s2);
                return Result.ok().message("点赞成功");
            }
        }else{
            redisTemplate.opsForHash().put("课程点赞",cid.toString(),"1");
        }
        return Result.ok();
    }

    // @GetMapping("/qxdz")
    // private ResultClasss qxdz(Integer cid, HttpServerRequest request){
    //     String token = request.getHeader("token");
    //     User user = JSON.parseObject(String.valueOf(redisTemplate.opsForHash().get("用户登录信息", token)), User.class);
    //     String o = (String) redisTemplate.opsForHash().get("课程点赞", String.valueOf(cid));
    //     o.replace("uid:"+user.getId(),"");
    //
    //
    //     return ResultClasss.ok(1).Message("取消成功");
    // }

    // @GetMapping("/adddz1")
    // private ResultClasss adddz1(Integer cid){
    //     Integer num = (Integer) redisTemplate.opsForHash().get("课程点赞数量", String.valueOf(cid));
    //     if(num==null){
    //         redisTemplate.opsForHash().put("课程点赞数量",String.valueOf(cid),1);
    //     }else{
    //         redisTemplate.opsForHash().put("课程点赞数量",String.valueOf(cid),num+1);
    //     }
    //     return ResultClasss.ok(1).Message("点赞成功");
    // }
}